<?php
/**
 * create to 2021/6/28 9:08
 */

namespace app\member\controller\api;

use app\ApiController;
use app\mainadmin\model\TaskingModel;
use app\mainadmin\model\TaskingLogModel;
/*------------------------------------------------------ */
//-- 任务相关API
/*------------------------------------------------------ */

class Tasking extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
//        $this->checkLogin();//验证登陆
        $this->Model = new TaskingModel();
        $this->taskLogModel = new TaskingLogModel();
    }

    /*------------------------------------------------------ */
    //-- 任务列表
    /*------------------------------------------------------ */
    public function taskList() {
        $user_id = $this->userInfo['user_id'];

        $list = $this->Model->where('status',1)->order('sort desc')->select();
        $start_time = strtotime(date("Y-m-d",time()));
        $end_time = time();
        foreach ($list as &$v) {
            $where = [
                'tasking_id'=>$v['id'],
                'user_id'=>$user_id
            ];
            switch ($v['type']) {
                case 1:
                    $taskingStatus = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->field('status')->find();
                    if ($taskingStatus) {
                        $v['tasking_status'] = $taskingStatus['status'];
                    } else {
                        $v['tasking_status'] = 0;
                    }
                break;
                case 2:
                    //$taskingStatus = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->field('status')->select();
                    $taskingStatus = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->field('status')->order('createtime desc')->find();
                    $v['count_task'] = count($taskingStatus);
                    $v['tasking_status'] = $taskingStatus['status'];
                break;
                case 3:
                    $taskingStatus = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->field('status')->select();
                    if ($taskingStatus) {
                        $v['tasking_status'] = $taskingStatus['status'];
                    } else {
                        $v['tasking_status'] = 0;
                    }
                break;
            }
        }
        return $this->success('获取成功','',$list);
    }

    /*------------------------------------------------------ */
    //-- 领取任务
    //-- taskStatus 0今日该任务领取完 1领取成功 2进行中
    /*------------------------------------------------------ */
    public function receiveTasking() {
        $tasking_id = input('tasking_id','','trim');
        $user_id = $this->userInfo['user_id'];
        if (empty($user_id)) {
            return $this->error('请先登录.');
        }
        if (empty($tasking_id)) {
            return $this->error('缺少参数.');
        }
        $tasking = $this->Model->where('id',$tasking_id)->find();
        if (!$tasking) {
            return $this->error('任务不存在.');
        }
        $start_time = strtotime(date("Y-m-d",time()));
        $end_time = time();
        $where = [
            'tasking_id'=>$tasking_id,
            'user_id'=>$user_id
        ];
        switch ($tasking['type']) {
            case 1: // 单次
                $taskingList = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->field('status')->find();
                if ($taskingList) {
                    if ($taskingList['status'] == 2 ) {
                        $taskStatus = 2;
                    } else {
                        $taskStatus = 0;
                    }
                } else {
                    $where['status'] = 2;
                    $where['createtime'] = time();
                    $this->taskLogModel->insert($where);
                    $taskStatus = 1;
                }
                break;
            case 2: // 多次
                $taskingCount = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->count();
                if ($taskingCount>=$tasking['task_nums']) {
                    $taskStatus = 0;
                } else {
                    $where['status'] = 2;
                    $taskingInfo = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->find();
                    if ($taskingInfo) {
                        $taskStatus = 2;
                    } else {
                        $where['status'] = 2;
                        $where['createtime'] = time();
                        $this->taskLogModel->insert($where);
                        $taskStatus = 1;
                    }
                }
                break;
            case 3: // 无限制
                $where['status'] = 2;
                $taskingInfo = $this->taskLogModel->whereBetweenTime('createtime',$start_time,$end_time)->where($where)->find();
                if ($taskingInfo) {
                    $taskStatus = 2;
                } else {
                    $where['status'] = 2;
                    $where['createtime'] = time();
                    $this->taskLogModel->insert($where);
                    $taskStatus = 1;
                }
                break;
        }
        $result = [
            'taskStatus'=>$taskStatus
        ];
        return $this->success('操作成功','',$result);
    }
}